<?php


namespace App\Http\Controllers\Admin;

use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

class SexController extends BaseController
{
    /**
     * @Desc:列表
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function list(Request $request)
    {
        $param = $request->only(['page', 'page_size', 'status', 'title']);
        $v = app('validator')->make($param, [
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1',
            'status' => 'filled|integer|in:0,1',
            'title' => 'filled|string',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $param['type'] = 1;
        $list = app('repo_erm_sex_and_love')->list($param);
        return Response::sendData($list);
    }

    /**
     * @Desc:详情
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function info(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|exists:mysql.ln_user_sex_and_love,id',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_erm_sex_and_love')->info($param);
        return Response::sendData($list);
    }

    /**
     * @Desc:新增
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function create(Request $request)
    {
        $param = $request->only([
            'title',
            'image',
            'explain',
            'icon'
        ]);
        $v = app('validator')->make($param, [
            'title' => 'required|string|min:1|max:6',
            'image' => 'required|string|max:255',
            'icon' => 'required|string|max:255',
            'explain' => 'filled|string|min:1|max:255',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $param['type'] = 1;
        app('repo_erm_sex_and_love')->create($param);
        return Response::sendData(true);
    }

    /**
     * @Desc:更新
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function update(Request $request)
    {
        $param = $request->only([
            'id',
            'title',
            'image',
            'explain',
            'icon'
        ]);
        $v = app('validator')->make($param, [
            'id' => 'required|integer|exists:mysql.ln_user_sex_and_love,id',
            'title' => 'required|string|min:1|max:6',
            'image' => 'required|string|max:255',
            'icon' => 'required|string|max:255',
            'explain' => 'filled|string|min:1|max:255',
        ]);

        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $list = app('repo_erm_sex_and_love')->update($param);
        return Response::sendData($list);
    }

    /**
     * @Desc:删除
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function delete(Request $request)
    {
        $param = $request->only(['id']);
        $v = app('validator')->make($param, [
            'id' => 'required|integer|exists:mysql.ln_user_sex_and_love,id',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        app('repo_erm_sex_and_love')->delete($param);
        return Response::sendData(true);
    }

    /**
     * @Desc:更新状态
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/8 14:59
     */
    public function updateStatus(Request $request)
    {
        $param = $request->only(['id', 'status']);
        $v = app('validator')->make($param, [
            'id' => 'required|integer|exists:mysql.ln_user_sex_and_love,id',
            'status' => 'required|in:0,1'
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        app('repo_erm_sex_and_love')->updateStatus($param);
        return Response::sendData(true);
    }


}